home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / apt-xapian-index / examples / axi-query-similar.py < prev    next >
Encoding:
Python Source  |  2008-04-23  |  2.9 KB  |  96 lines

  1. #!/usr/bin/python
  2.  
  3. # axi-query-similar - Show packages similar to a given one
  4. #
  5. # Copyright (C) 2007  Enrico Zini <enrico@debian.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  
  21. from optparse import OptionParser
  22. import sys
  23.  
  24. VERSION="0.1"
  25.  
  26. # Let's start with a simple command line parser with help
  27. class Parser(OptionParser):
  28.     def __init__(self, *args, **kwargs):
  29.         OptionParser.__init__(self, *args, **kwargs)
  30.  
  31.     def error(self, msg):
  32.         sys.stderr.write("%s: error: %s\n\n" % (self.get_prog_name(), msg))
  33.         self.print_help(sys.stderr)
  34.         sys.exit(2)
  35.  
  36. parser = Parser(usage="usage: %prog [options] package(s)",
  37.                 version="%prog "+ VERSION,
  38.                 description="Find the packages similar to the given ones")
  39.  
  40. (options, args) = parser.parse_args()
  41.  
  42.  
  43. # Import the rest here so we don't need dependencies to be installed only to
  44. # print commandline help
  45. import os
  46. import xapian
  47. from aptxapianindex import *
  48.  
  49.  
  50. # Instantiate a xapian.Database object for read only access to the index
  51. db = xapian.Database(XAPIANDB)
  52.  
  53. def docForPackage(pkgname):
  54.     "Get the document corresponding to the package with the given name"
  55.     # Query the term with the package name
  56.     query = xapian.Query("XP"+pkgname)
  57.     enquire = xapian.Enquire(db)
  58.     enquire.set_query(query)
  59.     # Get the top result only
  60.     matches = enquire.get_mset(0, 1)
  61.     if matches.size() == 0:
  62.         return None
  63.     else:
  64.         m = matches[0]
  65.         return m[xapian.MSET_DOCUMENT]
  66.  
  67. # Build a term list with all the terms in the given packages
  68. terms = []
  69. for pkgname in args:
  70.     # Get the document corresponding to the package name
  71.     doc = docForPackage(pkgname)
  72.     if not doc: continue
  73.     # Retrieve all the terms in the document
  74.     for t in doc.termlist():
  75.         if len(t.term) < 2 or t.term[:2] != 'XP':
  76.             terms.append(t.term)
  77.  
  78. # Build the big OR query
  79. query = xapian.Query(xapian.Query.OP_AND_NOT,
  80.             # Terms we want
  81.             xapian.Query(xapian.Query.OP_OR, terms),
  82.             # AND NOT the input packages
  83.             xapian.Query(xapian.Query.OP_OR, ["XP"+name for name in args]))
  84.  
  85. # Perform the query
  86. enquire = xapian.Enquire(db)
  87. enquire.set_query(query)
  88.  
  89. # Retrieve the top 20 results
  90. matches = enquire.get_mset(0, 20)
  91.  
  92. # Display the results
  93. show_mset(matches)
  94.  
  95. sys.exit(0)
  96.